/** @file SynchCommand
    @class SynchCommand
    @brief Command that syncs a player position and orientation
*/
#ifndef __SynchCommand_h_
#define __SynchCommand_h_

#include "Command.h"
#include <Windows.h>
class SynchCommand : public Command {
public:
	/**
	* The constructor
	*/
	SynchCommand (Ogre::Vector3 position,Ogre::Quaternion orientation, unsigned int playerId)
		: Command(playerId) {
		m_position = position;
		m_orientation = orientation;
	}
	
	/**
	* The empty constructor
	*/
	SynchCommand() {}
		
	/**
	* The deconstructor
	*/
	virtual ~SynchCommand (void) {}
	
	/**
	* Executs the command on argument model
	*/
	virtual bool execute(Model *model) {
		GameObject *gameObject = model->getByID(getPlayerId());
		if ( gameObject == NULL) {
			return false;
		} else {
			gameObject->synchPositionAndOrientation(m_position, m_orientation);
			//gameObject->getSceneNode()->setPosition(m_position);
			//gameObject->getSceneNode()->setOrientation(m_orientation);
			return true;
		}
	}
	
	/**
	* Saves the command to a output file stream
	*/
	virtual void save(ofstream &ofs) {
		int i = SYNCHCOMMAND;
		ofs.write((char*) &i, sizeof(int));
		Command::save(ofs);
		ofs.write((char*) &m_position, sizeof(Ogre::Vector3));
		ofs.write((char*) &m_orientation, sizeof(Ogre::Quaternion));
  }

	/**
	* Loads a command from a input file stream
	*/
  virtual void load(ifstream &ifs) {
		Command::load(ifs);
		ifs.read((char*) &m_position, sizeof(Ogre::Vector3));
		ifs.read((char*) &m_orientation, sizeof(Ogre::Quaternion));
  }
	
	/**
	* Saves the command to a bit stream
	*/
  virtual void save(RakNet::BitStream &ofs) {
		int i = SYNCHCOMMAND;
		ofs.Write((char*) &i, sizeof(int));
		Command::save(ofs);
		ofs.Write((char*) &m_position, sizeof(Ogre::Vector3));
		ofs.Write((char*) &m_orientation, sizeof(Ogre::Quaternion));
  }

	/**
	* Loads a command from a bit stream
	*/
  virtual void load(RakNet::BitStream &ifs) {
		Command::load(ifs);
		ifs.Write((char*) &m_position, sizeof(Ogre::Vector3));
		ifs.Write((char*) &m_orientation, sizeof(Ogre::Quaternion));
  }

private:
	/**
	* Position of the game object
	*/
	Ogre::Vector3 m_position;
	
	/**
	* Orientation of the game object
	*/
	Ogre::Quaternion m_orientation;
};
#endif